package com.wanjia.bpsapi.config;

import com.wanjia.bpsapi.constant.MapService;
import com.wanjia.bpsapi.properties.MapServiceProperties;
import com.wanjia.bpsapi.service.IMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.MissingRequiredPropertiesException;

import java.util.Optional;
import java.util.function.Supplier;

@Configuration
public class MapServiceConfig {

    @Bean
    public IMapService mapService(@Autowired MapServiceProperties properties) {
        return Optional.of(properties)
                .map(MapServiceProperties::getService)
                .map(MapService::getBuilder)
                .map(Supplier::get)
                .orElseThrow(() -> {
                    MissingRequiredPropertiesException e = new MissingRequiredPropertiesException();
                    e.getMissingRequiredProperties().add("map-service.service");
                    return e;
                });
    }
}
